package com.yx.changdao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.entity.CommunityBuild;
import com.yx.changdao.common.entity.ContactGroupBuild;
import com.yx.changdao.common.entity.DepContactGroup;
import com.yx.changdao.common.entity.SysUser;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.mapper.DepContactGroupMapper;
import com.yx.changdao.service.ContactGroupBuildService;
import com.yx.changdao.service.DepContactGroupService;
import com.yx.changdao.service.SysUserService;
import org.nutz.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: <br/>
 * 部门联户人小组
 * <p>
 * <br/>
 * @Auther: zcz
 * @create 2020/04/30 11:26
 */
@Service
public class DepContactGroupServiceImpl extends ServiceImpl<DepContactGroupMapper, DepContactGroup> implements DepContactGroupService {
    /**
     * 用户服务
     */
    @Autowired
    private SysUserService sysUserService;

    /**
     * 联户组楼中间
     */
    @Autowired
    private ContactGroupBuildService contactGroupBuildService;

    @Override
    public void contactLinkAccount(String contactGroupId, List<SysUser> addUserList) throws AlertException {
        DepContactGroup depContactGroup = getOne(new QueryWrapper<DepContactGroup>().eq(DepContactGroup.CONTACT_GROUP_ID, contactGroupId));
        if (null == depContactGroup) {
            throw new AlertException("未查询到联户组信息");
        }
        List<SysUser> userList = sysUserService.list(new QueryWrapper<SysUser>().eq(SysUser.DEP_ID, depContactGroup.getDepId()).orderByAsc(SysUser.USERNAME));
        for (SysUser sysUser : userList) { // 清空当前联户组的关联关系
            if (sysUser.getContactGroupId() != null && sysUser.getContactGroupId().intValue() == depContactGroup.getContactGroupId()) {
                sysUser.setContactGroupId(0);
                sysUser.setContactGroupName("");
            }
        }
        for (SysUser addUser : addUserList) {
            for (SysUser sysUser : userList) {
                //重新关联
                if (Strings.equals(addUser.getUserId(), sysUser.getUserId())) {
                    sysUser.setContactGroupId(depContactGroup.getContactGroupId());
                    sysUser.setContactGroupName(depContactGroup.getGroupName());
                }
            }
        }
        sysUserService.updateBatchById(userList);
    }

    @Override
    public void noContactLinkAccount(String contactGroupId, List<SysUser> addUserList) throws AlertException {
        DepContactGroup depContactGroup = getOne(new QueryWrapper<DepContactGroup>().eq(DepContactGroup.CONTACT_GROUP_ID, contactGroupId));
        if (null == depContactGroup) {
            throw new AlertException("未查询到联户组信息");
        }

        for (SysUser sysUser : addUserList) { // 清空当前联户组的关联关系
            if (sysUser.getContactGroupId() != null && sysUser.getContactGroupId().intValue() == depContactGroup.getContactGroupId()) {
                sysUser.setContactGroupId(0);
                sysUser.setContactGroupName("");
            }
        }
        sysUserService.updateBatchById(addUserList);
    }

    @Override
    public void contactLinkBuild(String contactGroupId, List<CommunityBuild> buildList) throws AlertException {
        DepContactGroup depContactGroup = getOne(new QueryWrapper<DepContactGroup>().eq(DepContactGroup.CONTACT_GROUP_ID, contactGroupId));
        if (null == depContactGroup) {
            throw new AlertException("未查询到联户组信息");
        }
        contactGroupBuildService.remove(new QueryWrapper<ContactGroupBuild>().eq(ContactGroupBuild.CONTACT_GROUP_ID,contactGroupId));
        List<ContactGroupBuild>  addContactBuildList = new ArrayList<>();
        for (CommunityBuild communityBuild : buildList) {
            ContactGroupBuild contactGroupBuild =new ContactGroupBuild();
            contactGroupBuild.setBuildId(communityBuild.getId());
            contactGroupBuild.setBuildName(communityBuild.getBuildName());
            contactGroupBuild.setContactGroupId(depContactGroup.getContactGroupId());
            contactGroupBuild.setContactGroupName(depContactGroup.getGroupName());
            addContactBuildList.add(contactGroupBuild);
        }
        contactGroupBuildService.saveBatch(addContactBuildList);
    }

    @Override
    public void noContactLinkBuild(String contactGroupId, List<CommunityBuild> buildList) throws AlertException {
        DepContactGroup depContactGroup = getOne(new QueryWrapper<DepContactGroup>().eq(DepContactGroup.CONTACT_GROUP_ID, contactGroupId));
        if (null == depContactGroup) {
            throw new AlertException("未查询到联户组信息");
        }
        for (CommunityBuild communityBuild : buildList) {
            contactGroupBuildService.remove(new QueryWrapper<ContactGroupBuild>().eq(ContactGroupBuild.CONTACT_GROUP_ID,contactGroupId)
                    .eq(ContactGroupBuild.BUILD_ID,communityBuild.getId()));
        }

    }
}
